package com.example.db.entity;

import lombok.Data;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.domain.Persistable;
import org.springframework.data.jpa.domain.AbstractPersistable;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.time.LocalDateTime;

import static javax.persistence.TemporalType.TIMESTAMP;

/**
 * @author L.xf
 * @date 2021/10/27
 */
@Data
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class) // 审计
public class BaseEntity implements Persistable<Long> {


    @Id
    @GeneratedValue(generator = "snowflakeIdentifierGenerator")
    @GenericGenerator(name = "snowflakeIdentifierGenerator", strategy = "com.example.db.generator.SnowflakeIdentifierGenerator")
    private Long id;

    @CreatedBy
    @Column(name = "create_user_id",updatable = false)
    private Long createUserId;

    @LastModifiedBy
    private Long updateUserId;

    @CreatedDate
    @Column(name = "create_time", updatable = false)
    private LocalDateTime createTime;

    @LastModifiedDate
//    @Temporal(TIMESTAMP)
    private LocalDateTime updateTime;


    @Override
    public boolean isNew() {
        return null == getId();
    }
}
